<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 7.3.0">

  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32.ico">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16.ico">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css" integrity="sha256-wiz7ZSCn/btzhjKDQBms9Hx4sSeUYsDrTLg7roPstac=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.28/fancybox/fancybox.css" integrity="sha256-6cQIC71/iBIYXFK+0RHAvwmjwWzkWd+r7v/BX3/vZDc=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/pace/1.2.4/themes/green/pace-theme-minimal.css">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/pace/1.2.4/pace.min.js" integrity="sha256-gqd7YTjg/BtfqWSwsJOvndl0Bxc8gFImLEkXQT8+qj0=" crossorigin="anonymous"></script>

<script class="next-config" data-name="main" type="application/json">{"hostname":"sumumm.github.io","root":"/","images":"/images","scheme":"Gemini","darkmode":false,"version":"8.19.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":{"enable":true,"style":"mac"},"fold":{"enable":true,"height":300},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":true,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":true,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script>

    <meta name="description" content="tasklet是什么？若笔记中有错误或者不合适的地方，欢迎批评指正😃。">
<meta property="og:type" content="article">
<meta property="og:title" content="LV06-13-中断-03-中断下半部-01-tasklet">
<meta property="og:url" content="https://sumumm.github.io/post/765d0409.html">
<meta property="og:site_name" content="苏木">
<meta property="og:description" content="tasklet是什么？若笔记中有错误或者不合适的地方，欢迎批评指正😃。">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV06-%E9%A9%B1%E5%8A%A8%E5%BC%80%E5%8F%91/LV06-13-%E4%B8%AD%E6%96%AD-03-%E4%B8%AD%E6%96%AD%E4%B8%8B%E5%8D%8A%E9%83%A8-01-tasklet/img/image-20250323122800703.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV06-%E9%A9%B1%E5%8A%A8%E5%BC%80%E5%8F%91/LV06-13-%E4%B8%AD%E6%96%AD-03-%E4%B8%AD%E6%96%AD%E4%B8%8B%E5%8D%8A%E9%83%A8-01-tasklet/img/image-20250323123123347.png">
<meta property="article:published_time" content="2025-03-23T10:40:41.000Z">
<meta property="article:modified_time" content="2025-06-13T16:25:57.056Z">
<meta property="article:author" content="苏木">
<meta property="article:tag" content="LV06-驱动开发">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV06-%E9%A9%B1%E5%8A%A8%E5%BC%80%E5%8F%91/LV06-13-%E4%B8%AD%E6%96%AD-03-%E4%B8%AD%E6%96%AD%E4%B8%8B%E5%8D%8A%E9%83%A8-01-tasklet/img/image-20250323122800703.png">


<link rel="canonical" href="https://sumumm.github.io/post/765d0409.html">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":"","permalink":"https://sumumm.github.io/post/765d0409.html","path":"post/765d0409.html","title":"LV06-13-中断-03-中断下半部-01-tasklet"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>LV06-13-中断-03-中断下半部-01-tasklet | 苏木</title>
  








    <script src="/js/browser_tools_disable.js"></script>

  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
<!-- hexo injector head_end start --><link rel="stylesheet" href="https://unpkg.com/hexo-next-tags-plus@latest/lib/tag_plus.css" media="defer" onload="this.media='all'"><!-- hexo injector head_end end --></head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">苏木</p>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">我的学习之路</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="搜索" role="button">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>苏木的家</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类页<span class="badge">42</span></a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档页<span class="badge">673</span></a></li><li class="menu-item menu-item-flink"><a href="/flink/" rel="section"><i class="fa fa-link fa-fw"></i>友人帐</a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于我</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%80%E3%80%81tasklet%E7%AE%80%E4%BB%8B"><span class="nav-text">一、tasklet简介</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-tasklet%E6%98%AF%E4%BB%80%E4%B9%88%EF%BC%9F"><span class="nav-text">1. tasklet是什么？</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E4%BC%98%E7%BC%BA%E7%82%B9"><span class="nav-text">2. 优缺点</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-%E6%80%8E%E4%B9%88%E6%8F%8F%E8%BF%B0%E4%B8%80%E4%B8%AAtasklet"><span class="nav-text">3. 怎么描述一个tasklet</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%BA%8C%E3%80%81%E7%89%B9%E6%AE%8A%E7%9A%84%E8%BD%AF%E4%B8%AD%E6%96%AD%E2%80%94%E2%80%94-tasklet-%E5%88%86%E6%9E%90"><span class="nav-text">二、特殊的软中断—— tasklet 分析</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-TASKLET-SOFTIRQ"><span class="nav-text">1. TASKLET_SOFTIRQ</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-1-%E8%A7%A6%E5%8F%91%E5%87%BD%E6%95%B0-tasklet-schedule"><span class="nav-text">1.1 触发函数 tasklet_schedule()</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-2-%E6%9C%8D%E5%8A%A1%E7%A8%8B%E5%BA%8Ftasklet-action"><span class="nav-text">1.2 服务程序tasklet_action()</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-HI-SOFTIRQ"><span class="nav-text">2. HI_SOFTIRQ</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%89%E3%80%81%E7%9B%B8%E5%85%B3api%E5%87%BD%E6%95%B0"><span class="nav-text">三、相关api函数</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E9%9D%99%E6%80%81%E5%88%9D%E5%A7%8B%E5%8C%96%E5%87%BD%E6%95%B0"><span class="nav-text">1. 静态初始化函数  </span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-1-%E4%B8%A4%E4%B8%AA%E5%AE%8F"><span class="nav-text">1.1 两个宏</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-2-%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B"><span class="nav-text">1.2 使用实例</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E5%8A%A8%E6%80%81%E5%88%9D%E5%A7%8B%E5%8C%96%E5%87%BD%E6%95%B0"><span class="nav-text">2. 动态初始化函数</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2-1-tasklet-init"><span class="nav-text">2.1 tasklet_init()</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-2-%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B"><span class="nav-text">2.2 使用实例</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-%E6%94%B9%E5%8F%98%E4%B8%80%E4%B8%AAtasklet%E7%9A%84%E7%8A%B6%E6%80%81"><span class="nav-text">3. 改变一个tasklet的状态</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#3-1-%E4%BD%BF%E8%83%BD-%E5%85%B3%E9%97%AD%E7%8A%B6%E6%80%81"><span class="nav-text">3.1 使能&#x2F;关闭状态 </span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#3-1-1-tasklet-enable"><span class="nav-text">3.1.1 tasklet_enable()</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-1-2-tasklet-disable"><span class="nav-text">3.1.2 tasklet_disable()</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-2-%E6%94%B9%E5%8F%98%E8%BF%90%E8%A1%8C%E7%8A%B6%E6%80%81"><span class="nav-text">3.2 改变运行状态</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-%E8%B0%83%E5%BA%A6%E5%87%BD%E6%95%B0"><span class="nav-text">4. 调度函数  </span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#4-1-tasklet-schedule"><span class="nav-text">4.1 tasklet_schedule()</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-2-%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B"><span class="nav-text">4.2 使用实例</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#5-%E9%94%80%E6%AF%81%E5%87%BD%E6%95%B0"><span class="nav-text">5. 销毁函数  </span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#5-1-tasklet-kill"><span class="nav-text">5.1 tasklet_kill()</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#5-2-%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B"><span class="nav-text">5.2 使用实例</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%9B%9B%E3%80%81tasklet-%E9%98%9F%E5%88%97"><span class="nav-text">四、tasklet 队列</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%BA%94%E3%80%81%E4%BD%BF%E7%94%A8%E6%80%BB%E7%BB%93"><span class="nav-text">五、使用总结</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E4%BD%BF%E7%94%A8%E6%AD%A5%E9%AA%A4"><span class="nav-text">1. 使用步骤</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E7%AE%80%E5%8D%95%E7%A4%BA%E4%BE%8B"><span class="nav-text">2. 简单示例</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%85%AD%E3%80%81tasklet%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B"><span class="nav-text">六、tasklet使用实例</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-demo%E6%BA%90%E7%A0%81"><span class="nav-text">1. demo源码</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E5%BC%80%E5%8F%91%E6%9D%BF%E6%B5%8B%E8%AF%95"><span class="nav-text">2. 开发板测试</span></a></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="苏木"
      src="/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">苏木</p>
  <div class="site-description" itemprop="description">莫道桑榆晚，为霞尚满天</div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">673</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">42</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
        <span class="site-state-item-count">43</span>
        <span class="site-state-item-name">标签</span>
      </div>
  </nav>
</div>
  <div class="links-of-author animated">
      <span class="links-of-author-item">
        <a href="https://github.com/sumumm" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;sumumm" rel="noopener me" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
  </div>

        </div>
      </div>
    </div>

    
  </aside>


    </div>

    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://sumumm.github.io/post/765d0409.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="苏木">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="苏木">
      <meta itemprop="description" content="莫道桑榆晚，为霞尚满天">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="LV06-13-中断-03-中断下半部-01-tasklet | 苏木">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          LV06-13-中断-03-中断下半部-01-tasklet
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2025-03-23 18:40:41" itemprop="dateCreated datePublished" datetime="2025-03-23T18:40:41+08:00">2025-03-23</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/" itemprop="url" rel="index"><span itemprop="name">嵌入式开发</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/" itemprop="url" rel="index"><span itemprop="name">02IMX6ULL平台</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV06-%E9%A9%B1%E5%8A%A8%E5%BC%80%E5%8F%91/" itemprop="url" rel="index"><span itemprop="name">LV06-驱动开发</span></a>
        </span>
    </span>

  
    <span class="post-meta-break"></span>
    <span class="post-meta-item" title="本文字数">
      <span class="post-meta-item-icon">
        <i class="far fa-file-word"></i>
      </span>
      <span class="post-meta-item-text">本文字数：</span>
      <span>7.8k</span>
    </span>
    <span class="post-meta-item" title="阅读时长">
      <span class="post-meta-item-icon">
        <i class="far fa-clock"></i>
      </span>
      <span class="post-meta-item-text">阅读时长 &asymp;</span>
      <span>29 分钟</span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody"><p>tasklet是什么？若笔记中有错误或者不合适的地方，欢迎批评指正😃。</p>
<span id="more"></span>

<!-- Photo: https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV06-%E9%A9%B1%E5%8A%A8%E5%BC%80%E5%8F%91/LV06-13-%E4%B8%AD%E6%96%AD-03-%E4%B8%AD%E6%96%AD%E4%B8%8B%E5%8D%8A%E9%83%A8-01-tasklet/img/ -->

<details class="folding-tag" blue><summary> 点击查看使用工具及版本 </summary>
              <div class='content'>
              <table>    <tr>        <td align="center" rowspan="5">PC端开发环境</td>        <td align="center" width=150px>Windows</td>        <td align="left">Windows11</td>    </tr>    <tr>        <td align="center">Ubuntu</td>        <td align="left">Ubuntu20.04.2的64位版本</td>      </tr>    <tr>        <td align="center">VMware® Workstation 17 Pro</td>        <td align="left">17.6.0 build-24238078</td>      </tr>    <tr>        <td align="center">终端软件</td>        <td align="left">MobaXterm(Professional Edition v23.0 Build 5042 (license))</td>    </tr>    <tr>        <td align="center">Win32DiskImager</td>        <td align="left">Win32DiskImager v1.0</td>      </tr>    <tr>        <td align="center" rowspan="3">Linux开发板环境</td>        <td align="center">Linux开发板</td>        <td align="left">正点原子 i.MX6ULL Linux 阿尔法开发板</td>      </tr>    <tr>        <td align="center">uboot</td>        <td align="left">NXP官方提供的uboot，使用的uboot版本为U-Boot 2019.04</td>      </tr>    <tr>        <td align="center">linux内核</td>        <td align="left">linux-4.19.71(NXP官方提供)</td>      </tr></table>
              </div>
            </details>

<details class="folding-tag" blue><summary> 点击查看本文参考资料 </summary>
              <div class='content'>
              <table>    <tr>        <td align="center">分类</td>        <td align="center">网址</td>        <td align="center">说明</td>    </tr>    <tr>        <td align="center" rowspan="5">官方网站</td>        <td align="left"><a href="https://www.arm.com/" target="_blank">https://www.arm.com/</a></td>        <td align="left">ARM官方网站，在这里我们可以找到Cotex-Mx以及ARMVx的一些文档</td>    </tr>    <tr>        <td align="left"><a href="https://www.nxp.com.cn/" target="_blank">https://www.nxp.com.cn/ </a></td>        <td align="left">NXP官方网站</td>    </tr>    <tr>        <td align="left"><a href="https://www.nxpic.org.cn/" target="_blank">https://www.nxpic.org.cn/</a></td><td align="left">NXP 官方社区</td>    </tr>    <tr>        <td align="left"><a href="https://u-boot.readthedocs.io/en/latest/" target="_blank">https://u-boot.readthedocs.io/en/latest/</a></td><td align="left">u-boot官网</td>    </tr>    <tr>        <td align="left"><a href="https://www.kernel.org/" target="_blank">https://www.kernel.org/</a></td><td align="left">linux内核官网</td>    </tr></table>
              </div>
            </details>

<details class="folding-tag" blue><summary> 点击查看相关文件下载 </summary>
              <div class='content'>
              <table>    <tr>        <td align="center">分类</td>        <td align="center">网址</td>        <td align="center">说明</td>    </tr>    <tr>        <td align="center" rowspan="3">NXP</td>        <td align="left"><a href="https://github.com/nxp-imx" target="_blank">https://github.com/nxp-imx</a></td>        <td align="left">NXP imx开发资源GitHub组织，里边会有u-boot和linux内核的仓库</td>    </tr>    <tr>        <td align="left"><a href="https://github.com/nxp-imx/linux-imx/releases/tag/v4.19.71" target="_blank">nxp-imx/linux-imx/releases/tag/v4.19.71</a></td>        <td align="left">NXP linux内核仓库tags中的v4.19.71</td>    </tr>    <tr>        <td align="left"><a href="https://github.com/nxp-imx/uboot-imx/releases/tag/rel_imx_4.19.35_1.1.0" target="_blank">nxp-imx/uboot-imx/releases/tag/rel_imx_4.19.35_1.1.0</a></td>        <td align="left">NXP u-boot仓库tags中的rel_imx_4.19.35_1.1.0</td>    </tr>    <tr>        <td align="center" rowspan="2">I.MX6ULL</td>        <td align="left"><a href="https://www.nxp.com.cn/docs/en/data-sheet/IMX6ULLIEC.pdf" target="_blank">i.MX 6ULL Applications Processors for Industrial Products</a></td>        <td align="left">I.MX6ULL 芯片手册（datasheet，可以在线查看）</td>    </tr>    <tr>        <td align="left"><a href="https://www.nxp.com.cn/webapp/Download?colCode=IMX6ULLRM&lang_cd=zh" target="_blank">i.MX 6ULL Applications ProcessorReference Manual</a></td>        <td align="left">I.MX6ULL 参考手册（下载后才能查看，需要登录NXP官网）</td>    </tr>    <tr>        <td align="center" rowspan="3">Source Code</td>        <td align="left"><a href="https://elixir.bootlin.com/linux/latest/source" target="_blank">https://elixir.bootlin.com/linux/latest/source</a></td>        <td align="left">linux kernel源码</td>    </tr>    <tr>        <td align="left"><a href="https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/?h=v4.19.71&id=e7d2672c66e4d3675570369bf20856296da312c4" target="_blank">kernel/git/stable/linux.git - Linux kernel stable tree</a></td>        <td align="left">linux kernel源码(官网,tag 4.19.71)</td>    </tr>    <tr>        <td align="left"><a href="https://elixir.bootlin.com/u-boot/latest/source" target="_blank">https://elixir.bootlin.com/u-boot/latest/source</a></td>        <td align="left">uboot源码</td>    </tr></table>
              </div>
            </details>

<h1 id="一、tasklet简介"><a href="#一、tasklet简介" class="headerlink" title="一、tasklet简介"></a><font size=3>一、tasklet简介</font></h1><p>下半部要做的事情耗时不是太长的时候，我们就可以考虑使用tasklet。接下来就来了解一下吧。  </p>
<h2 id="1-tasklet是什么？"><a href="#1-tasklet是什么？" class="headerlink" title="1. tasklet是什么？"></a><font size=3>1. tasklet是什么？</font></h2><p>在 Linux 内核中， tasklet 是一种特殊的<strong>软中断机制</strong>，它是通过软中断控制结构来实现的 ，被广泛用于处理中断下文相关的任务。 tasklet一词的原意是“小片任务”的意思，这里是指一小段可执行的代码，且通常以函数的形式出现。软中断向量<a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/include/linux/interrupt.h#L465">HI_SOFTIRQ</a>和<a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/include/linux/interrupt.h#L471">TASKLET_SOFTIRQ</a>均是用tasklet机制来实现的。</p>
<p>（1）与一般的软中断不同，某一段tasklet代码在某个时刻只能在一个CPU上运行，而不像一般的软中断服务函数(即softirq_action结构中的action函数指针)那样——在同一时刻可以被多个CPU并发地执行。因此不会出现并发冲突。</p>
<p>（2）不同的tasklet代码在同一时刻可以在多个CPU上并发地执行，而不像BH机制那样必须严格地串行化执行(也即在同一时刻系统中只能有一个CPU执行BH函数)。</p>
<p>（3） 需要注意的是， tasklet 绑定的函数中不能调用可能导致休眠的函数， 否则可能引起内核异常。</p>
<h2 id="2-优缺点"><a href="#2-优缺点" class="headerlink" title="2. 优缺点"></a><font size=3>2. 优缺点</font></h2><ul>
<li>优点</li>
</ul>
<p>（1）简化的接口和编程模型： tasklet 提供了一个简单的接口和编程模型， 使得在内核中处理延迟工作变得更加容易。 相比自己添加软中断， tasklet 提供了更高级的抽象。</p>
<p>（2）低延迟： tasklet 在软中断上下文中执行， 避免了内核线程的上下文切换开销， 因此具有较低的延迟。 这对于需要快速响应的延迟敏感任务非常重要。</p>
<p>（3）自适应调度： tasklet 具有自适应调度的特性， 当多个 tasklet 处于等待状态时， 内核会合并它们以减少不必要的上下文切换。 这种调度机制可以提高系统的效率。</p>
<ul>
<li>缺点</li>
</ul>
<p>（1）无法处理长时间运行的任务： tasklet 适用于短时间运行的延迟工作， 如果需要处理长时间运行的任务， 可能会阻塞其他任务的执行。 对于较长的操作， 可能需要使用工作队列或内核线程来处理。</p>
<p>（2）缺乏灵活性： tasklet 的执行受限于软中断的上下文， 不适用于所有类型的延迟工作。某些情况下， 可能需要更灵活的调度和执行机制， 这时自定义软中断可能更加适合。</p>
<p>（3）资源限制： tasklet 的数量是有限的， 系统中可用的 tasklet 数量取决于架构和内核配置。 如果需要大量的延迟工作处理， 可能会受到 tasklet 数量的限制。  </p>
<h2 id="3-怎么描述一个tasklet"><a href="#3-怎么描述一个tasklet" class="headerlink" title="3. 怎么描述一个tasklet"></a><font size=3>3. 怎么描述一个tasklet</font></h2><p>Linux用数据结构 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/include/linux/interrupt.h#L542">tasklet_struct</a> 来描述一个tasklet，每个结构代表一个独立的小任务。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">tasklet_struct</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">	<span class="class"><span class="keyword">struct</span> <span class="title">tasklet_struct</span> *<span class="title">next</span>;</span></span><br><span class="line">	<span class="type">unsigned</span> <span class="type">long</span> state;</span><br><span class="line">	<span class="type">atomic_t</span> count;</span><br><span class="line">	<span class="type">void</span> (*func)(<span class="type">unsigned</span> <span class="type">long</span>);</span><br><span class="line">	<span class="type">unsigned</span> <span class="type">long</span> data;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<ul>
<li>next：指向下一个tasklet 的指针， 用于形成链表结构， 以便内核中可以同时管理多个tasklet。</li>
<li>state：表示 tasklet 的当前状态。</li>
</ul>
<p>这一个32位的无符号长整数，当前只使用了bit[1]和bit[0]两个状态位。对这两个状态位的定义(在<a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/include/linux/interrupt.h#L558">interrupt.h</a>中)如下所示：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">enum</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">	TASKLET_STATE_SCHED,	<span class="comment">/* Tasklet is scheduled for execution */</span></span><br><span class="line">	TASKLET_STATE_RUN	<span class="comment">/* Tasklet is running (SMP only) */</span></span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<p>（1）bit0 表示 TASKLET_STATE_SCHED，等于 1 时表示已经执行了 tasklet_schedule 把该 tasklet 放入队列了； tasklet_schedule 会判断该位，如果已经等于 1 那么它就不会再次把tasklet 放入队列。（等于1时表示这个tasklet已经被调度去等待执行了。）  </p>
<p>（2）bit1 表示 TASKLET_STATE_RUN，bit[1]＝1 表示这个tasklet的func函数当前正在某个CPU上被执行，它仅对SMP系统才有意义，其作用就是为了防止多个CPU同时执行一个tasklet的情形出现。函数执行完后内核会把该位清 0。  </p>
<ul>
<li>count：用于引用计数， 用于确保 tasklet 在多个地方调度或取消调度时的正确处理。</li>
</ul>
<p>只有当count等于0时，tasklet代码段才能执行，也即此时tasklet是被使能的；如果count非零，则这个tasklet是被禁止的。任何想要执行一个tasklet代码段的人都首先必须先检查其count成员是否为0。</p>
<ul>
<li>func：指向 tasklet 绑定的函数的指针， 该函数将在 tasklet 执行时被调用。</li>
<li>data：传递给 tasklet 绑定函数的参数。这是一个32位的无符号整数，其具体含义可供func函数自行解释，比如将其解释成一个指向某个用户自定义数据结构的地址值。</li>
</ul>
<h1 id="二、特殊的软中断——-tasklet-分析"><a href="#二、特殊的软中断——-tasklet-分析" class="headerlink" title="二、特殊的软中断—— tasklet 分析"></a><font size=3>二、特殊的软中断—— tasklet 分析</font></h1><p>tasklet 是 Linux 内核中的一种软中断机制， 它可以被看作是一种轻量级的延迟处理机制。它是通过软中断控制结构来实现的， 因此也被称为软中断。</p>
<p>我们来从代码层面分析一下为什么 tasklet 是一个特殊的软中断呢？接下来就来看一下，Linux中有两个软中断向量<a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/include/linux/interrupt.h#L465">HI_SOFTIRQ</a>和<a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/include/linux/interrupt.h#L471">TASKLET_SOFTIRQ</a>，并且为他们实现了专用的触发函数和软中断服务函数。</p>
<ul>
<li>专用的触发函数</li>
</ul>
<p><a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/include/linux/interrupt.h#L588">tasklet_schedule()</a>函数和<a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/include/linux/interrupt.h#L596">tasklet_hi_schedule()</a>函数分别用来在当前CPU上触发软中断向量<a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/include/linux/interrupt.h#L471">TASKLET_SOFTIRQ</a>和<a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/include/linux/interrupt.h#L465">HI_SOFTIRQ</a>，并把指定的tasklet加入当前CPU所对应的tasklet队列中去等待执行。</p>
<ul>
<li>专用的软中断服务函数</li>
</ul>
<p><a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/kernel/softirq.c#L538">tasklet_action()</a>函数和<a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/kernel/softirq.c#L543">tasklet_hi_action()</a>函数则分别是软中断向量<a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/include/linux/interrupt.h#L471">TASKLET_SOFTIRQ</a>和<a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/include/linux/interrupt.h#L465">HI_SOFTIRQ</a>的软中断服务函数。在初始化函数<a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/kernel/softirq.c#L625">softirq_init()</a>中，这两个软中断向量对应的描述符softirq_vec[0]和softirq_vec[6]中的action函数指针就被分别初始化成指向函数<a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/kernel/softirq.c#L543">tasklet_hi_action()</a>和函数<a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/kernel/softirq.c#L538">tasklet_action()</a>。</p>
<p>可以来看一下初始化函数<a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/kernel/softirq.c#L625">softirq_init()</a>：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">void</span> <span class="title function_">open_softirq</span><span class="params">(<span class="type">int</span> nr, <span class="type">void</span> (*action)(<span class="keyword">struct</span> softirq_action *))</span></span><br><span class="line">&#123;</span><br><span class="line">	softirq_vec[nr].action = action;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">void</span> __init <span class="title function_">softirq_init</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">int</span> cpu;</span><br><span class="line">	<span class="comment">// 初始化每个可能的 CPU 的 tasklet_vec 和 tasklet_hi_vec</span></span><br><span class="line">    <span class="comment">// 将 tail 指针设置为对应的 head 指针的初始位置，这样做是为了确保 tasklet_vec 和 tasklet_hi_vec 的初始状态是空的。</span></span><br><span class="line">	for_each_possible_cpu(cpu) &#123;</span><br><span class="line">		per_cpu(tasklet_vec, cpu).tail =</span><br><span class="line">			&amp;per_cpu(tasklet_vec, cpu).head;</span><br><span class="line">		per_cpu(tasklet_hi_vec, cpu).tail =</span><br><span class="line">			&amp;per_cpu(tasklet_hi_vec, cpu).head;</span><br><span class="line">	&#125;</span><br><span class="line">    <span class="comment">// 注册 TASKLET_SOFTIRQ 软中断， 并指定对应的处理函数为 tasklet_action</span></span><br><span class="line">	open_softirq(TASKLET_SOFTIRQ, tasklet_action);</span><br><span class="line">    <span class="comment">// 注册 HI_SOFTIRQ 软中断， 并指定对应的处理函数为 tasklet_hi_action</span></span><br><span class="line">	open_softirq(HI_SOFTIRQ, tasklet_hi_action);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="1-TASKLET-SOFTIRQ"><a href="#1-TASKLET-SOFTIRQ" class="headerlink" title="1. TASKLET_SOFTIRQ"></a><font size=3>1. <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/include/linux/interrupt.h#L471">TASKLET_SOFTIRQ</a></font></h2><h3 id="1-1-触发函数-tasklet-schedule"><a href="#1-1-触发函数-tasklet-schedule" class="headerlink" title="1.1 触发函数 tasklet_schedule()"></a><font size=3>1.1 触发函数 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/include/linux/interrupt.h#L588">tasklet_schedule()</a></font></h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">static</span> <span class="keyword">inline</span> <span class="type">void</span> <span class="title function_">tasklet_schedule</span><span class="params">(<span class="keyword">struct</span> tasklet_struct *t)</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="keyword">if</span> (!test_and_set_bit(TASKLET_STATE_SCHED, &amp;t-&gt;state))</span><br><span class="line">		__tasklet_schedule(t);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>调用<a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/arch/arm/include/asm/bitops.h#L192">test_and_set_bit()</a>函数将待调度的tasklet的state成员变量的bit[0]位（也即TASKLET_STATE_SCHED位）设置为1，该函数同时还返回TASKLET_STATE_SCHED位的原有值。因此如果bit[0]为的原有值已经为1，那就说明这个tasklet已经被调度到另一个CPU上去等待执行了。由于一个tasklet在<strong>某一个时刻只能由一个CPU来执行</strong>，因此<a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/include/linux/interrupt.h#L588">tasklet_schedule()</a>函数什么也不做就直接返回了。否则，就继续下面的调度操作。我们来看一下下面调用的<a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/kernel/softirq.c#L486">__tasklet_schedule()</a>函数：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">void</span> __tasklet_schedule(<span class="keyword">struct</span> tasklet_struct *t)</span><br><span class="line">&#123;</span><br><span class="line">	__tasklet_schedule_common(t, &amp;tasklet_vec,</span><br><span class="line">				  TASKLET_SOFTIRQ);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>继续看这个 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/kernel/softirq.c#L470">__tasklet_schedule_common()</a>：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">static</span> <span class="type">void</span> __tasklet_schedule_common(<span class="keyword">struct</span> tasklet_struct *t,</span><br><span class="line">				      <span class="keyword">struct</span> tasklet_head __percpu *headp,</span><br><span class="line">				      <span class="type">unsigned</span> <span class="type">int</span> softirq_nr)</span><br><span class="line">&#123;</span><br><span class="line">	<span class="class"><span class="keyword">struct</span> <span class="title">tasklet_head</span> *<span class="title">head</span>;</span></span><br><span class="line">	<span class="type">unsigned</span> <span class="type">long</span> flags;</span><br><span class="line"></span><br><span class="line">	local_irq_save(flags);     <span class="comment">// 保存当前中断状态， 并禁用本地中断</span></span><br><span class="line">	head = this_cpu_ptr(headp);<span class="comment">// 获取当前 CPU 的 tasklet_head 指针</span></span><br><span class="line">	t-&gt;next = <span class="literal">NULL</span>;</span><br><span class="line">	*head-&gt;tail = t;           <span class="comment">// 将当前 tasklet 添加到 tasklet_head 的尾部</span></span><br><span class="line">	head-&gt;tail = &amp;(t-&gt;next);   <span class="comment">// 更新 tasklet_head 的尾指针</span></span><br><span class="line">	raise_softirq_irqoff(softirq_nr);<span class="comment">// 触发指定的软中断</span></span><br><span class="line">	local_irq_restore(flags);  <span class="comment">// 恢复中断状态</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li><p>首先，调用<a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/include/linux/irqflags.h#L113">local_irq_save()</a>函数来关闭当前CPU的中断，以保证下面的步骤在当前CPU上原子地被执行。</p>
</li>
<li><p>然后，将待调度的tasklet添加到当前CPU对应的tasklet队列的尾部。</p>
</li>
<li><p>接着，调用<a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/kernel/softirq.c#L422">raise_softirq_irqoff()</a>函数在当前CPU上触发软中断请求 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/include/linux/interrupt.h#L471">TASKLET_SOFTIRQ</a>。</p>
</li>
<li><p>最后，调用<a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/include/linux/irqflags.h#L120">local_irq_restore()</a>函数来开当前CPU的中断。.</p>
</li>
</ul>
<p><a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/kernel/softirq.c#L470">__tasklet_schedule_common()</a>函数将 tasklet 成功添加到链表的末尾。当软中断被触发时， 系统会遍历链表并处理每个 tasklet。 因此， 在添加到链表后， tasklet将在适当的时机被系统调度和执行。    </p>
<blockquote>
<p>Tips:</p>
<p>（1）tasklet_schedule 调度 tasklet 时，其中的函数并不会立刻执行，而只是把tasklet 放入队列；</p>
<p>（2）调用一次 tasklet_schedule，只会导致 tasklnet 的函数被执行一次；</p>
<p>（3）如果 tasklet 的函数尚未执行，多次调用 tasklet_schedule 也是无效的，只会放入队列一次。</p>
</blockquote>
<h3 id="1-2-服务程序tasklet-action"><a href="#1-2-服务程序tasklet-action" class="headerlink" title="1.2 服务程序tasklet_action()"></a><font size=3>1.2 服务程序<a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/kernel/softirq.c#L538">tasklet_action()</a></font></h3><p>函数<a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/kernel/softirq.c#L538">tasklet_action()</a>是tasklet机制与软中断向量<a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/include/linux/interrupt.h#L471">TASKLET_SOFTIRQ</a>的联系纽带。正是该函数将当前CPU的tasklet队列中的各个tasklet放到当前CPU上来执行的。</p>
<p>当发生硬件中断时，内核处理完硬件中断后，会处理软件中断。对于TASKLET_SOFTIRQ 软件中断，会调用 tasklet_action 函数。执行过程还是挺简单的：从队列中找到 tasklet，进行状态判断后执行 func函数，从队列中删除 tasklet。  </p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">static</span> __latent_entropy <span class="type">void</span> <span class="title function_">tasklet_action</span><span class="params">(<span class="keyword">struct</span> softirq_action *a)</span></span><br><span class="line">&#123;</span><br><span class="line">	tasklet_action_common(a, this_cpu_ptr(&amp;tasklet_vec), TASKLET_SOFTIRQ);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>我们来看 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/kernel/softirq.c#L500">tasklet_action_common()</a> 函数：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">static</span> <span class="type">void</span> <span class="title function_">tasklet_action_common</span><span class="params">(<span class="keyword">struct</span> softirq_action *a,</span></span><br><span class="line"><span class="params">				  <span class="keyword">struct</span> tasklet_head *tl_head,</span></span><br><span class="line"><span class="params">				  <span class="type">unsigned</span> <span class="type">int</span> softirq_nr)</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="class"><span class="keyword">struct</span> <span class="title">tasklet_struct</span> *<span class="title">list</span>;</span></span><br><span class="line"></span><br><span class="line">	local_irq_disable(); <span class="comment">// 禁用本地中断</span></span><br><span class="line">	<span class="built_in">list</span> = tl_head-&gt;head;<span class="comment">// 获取 tasklet_head 中的任务链表</span></span><br><span class="line">	tl_head-&gt;head = <span class="literal">NULL</span>;<span class="comment">// 清空 tasklet_head 中的任务链表</span></span><br><span class="line">	tl_head-&gt;tail = &amp;tl_head-&gt;head;<span class="comment">// 将 tail 指针重新指向 head 指针的位置</span></span><br><span class="line">	local_irq_enable();<span class="comment">// 启用本地中断</span></span><br><span class="line">	<span class="comment">// 遍历任务链表， 处理每一个 tasklet</span></span><br><span class="line">	<span class="keyword">while</span> (<span class="built_in">list</span>) &#123;</span><br><span class="line">		<span class="class"><span class="keyword">struct</span> <span class="title">tasklet_struct</span> *<span class="title">t</span> =</span> <span class="built_in">list</span>;</span><br><span class="line"></span><br><span class="line">		<span class="built_in">list</span> = <span class="built_in">list</span>-&gt;next;<span class="comment">// 获取下一个 tasklet， 并更新链表</span></span><br><span class="line"></span><br><span class="line">		<span class="keyword">if</span> (tasklet_trylock(t)) &#123;         <span class="comment">// 尝试获取 tasklet 的锁</span></span><br><span class="line">			<span class="keyword">if</span> (!<span class="type">atomic_read</span>(&amp;t-&gt;count)) &#123;<span class="comment">// 检查 count 计数器是否为 0</span></span><br><span class="line">				<span class="keyword">if</span> (!test_and_clear_bit(TASKLET_STATE_SCHED,</span><br><span class="line">							&amp;t-&gt;state))</span><br><span class="line">					BUG();<span class="comment">// 如果 state 标志位不正确， 则发生错误</span></span><br><span class="line">				t-&gt;func(t-&gt;data);  <span class="comment">// 执行 tasklet 的处理函数</span></span><br><span class="line">				tasklet_unlock(t); <span class="comment">// 解锁 tasklet</span></span><br><span class="line">				<span class="keyword">continue</span>;</span><br><span class="line">			&#125;</span><br><span class="line">			tasklet_unlock(t);</span><br><span class="line">		&#125;</span><br><span class="line"></span><br><span class="line">		local_irq_disable();<span class="comment">// 禁用本地中断</span></span><br><span class="line">		t-&gt;next = <span class="literal">NULL</span>;</span><br><span class="line">		*tl_head-&gt;tail = t;<span class="comment">// 将当前 tasklet 添加到 tasklet_head 的尾部</span></span><br><span class="line">		tl_head-&gt;tail = &amp;t-&gt;next;<span class="comment">// 更新 tail 指针</span></span><br><span class="line">		__raise_softirq_irqoff(softirq_nr);<span class="comment">// 触发软中断</span></span><br><span class="line">		local_irq_enable();<span class="comment">// 启用本地中断</span></span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>首先，在当前CPU关中断的情况下，“原子”地读取当前CPU的tasklet队列头部指针，将其保存到局部变量list指针中，然后将当前CPU的tasklet队列头部指针设置为NULL，以表示理论上当前CPU将不再有tasklet需要执行（但最后的实际结果却并不一定如此，下面将会看到）。</li>
<li>然后，用一个while{}循环来遍历由list所指向的tasklet队列，队列中的各个元素就是将在当前CPU上执行的tasklet。循环体的执行步骤如下：</li>
</ul>
<p>（1）用指针t来表示当前队列元素，即当前需要执行的tasklet。</p>
<p>（2）更新list指针为list-&gt;next，使它指向下一个要执行的tasklet。</p>
<p>（3）用<a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/include/linux/interrupt.h#L565">tasklet_trylock()</a>宏试图对当前要执行的tasklet（由指针t所指向）进行加锁，如果加锁成功（当前没有任何其他CPU正在执行这个tasklet），则用原子读函数<a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/include/asm-generic/atomic.h#L175">atomic_read()</a>进一步判断count成员的值。如果count为0，说明这个tasklet是允许执行的，于是：</p>
<p>① 先清除TASKLET_STATE_SCHED位；</p>
<p>② 然后，调用这个tasklet的可执行函数func；</p>
<p>③ 调用宏tasklet_unlock()来清除TASKLET_STATE_RUN位； </p>
<p>④ 最后，执行 continue 语句跳过下面的步骤，回到 while 循环继续遍历队列中的下一个元素。如果 count 不为0，说明这个 tasklet 是禁止运行的，于是调用tasklet_unlock() 清除前面用 tasklet_trylock() 设置的TASKLET_STATE_RUN 位。</p>
<h2 id="2-HI-SOFTIRQ"><a href="#2-HI-SOFTIRQ" class="headerlink" title="2. HI_SOFTIRQ"></a><font size=3>2. <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/include/linux/interrupt.h#L465">HI_SOFTIRQ</a></font></h2><p><a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/include/linux/interrupt.h#L465">HI_SOFTIRQ</a>相关函数都同理。</p>
<h1 id="三、相关api函数"><a href="#三、相关api函数" class="headerlink" title="三、相关api函数"></a><font size=3>三、相关api函数</font></h1><h2 id="1-静态初始化函数"><a href="#1-静态初始化函数" class="headerlink" title="1. 静态初始化函数  "></a><font size=3>1. 静态初始化函数  </font></h2><h3 id="1-1-两个宏"><a href="#1-1-两个宏" class="headerlink" title="1.1 两个宏"></a><font size=3>1.1 两个宏</font></h3><p>在 Linux 内核中， 有一个用于静态初始化 tasklet 的宏函数：<a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/include/linux/interrupt.h#L551">DECLARE_TASKLET</a>。 这个宏函数可以帮助我们更方便地进行 tasklet 的静态初始化。  </p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">define</span> DECLARE_TASKLET(name, func, data) \</span></span><br><span class="line"><span class="meta">struct tasklet_struct name = &#123; NULL, 0, ATOMIC_INIT(0), func, data &#125;</span></span><br></pre></td></tr></table></figure>

<p>其中， name 是 tasklet 的名称， func 是 tasklet 的处理函数， data 是传递给处理函数的参数。初始化状态为使能状态。如果 tasklet 初始化函数为非使能状态， 使用 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/include/linux/interrupt.h#L554">DECLARE_TASKLET_DISABLED</a> ：  </p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">define</span> DECLARE_TASKLET_DISABLED(name, func, data) \</span></span><br><span class="line"><span class="meta">struct tasklet_struct name = &#123; NULL, 0, ATOMIC_INIT(1), func, data &#125;</span></span><br></pre></td></tr></table></figure>

<p>其中， name 是 tasklet 的名称， func 是 tasklet 的处理函数， data 是传递给处理函数的参数。初始化状态为非使能状态。  </p>
<h3 id="1-2-使用实例"><a href="#1-2-使用实例" class="headerlink" title="1.2 使用实例"></a><font size=3>1.2 使用实例</font></h3><p>下面是一个实例：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;linux/interrupt.h&gt;</span></span></span><br><span class="line"><span class="comment">// 定义 tasklet 处理函数</span></span><br><span class="line"><span class="type">void</span> <span class="title function_">my_tasklet_handler</span><span class="params">(<span class="type">unsigned</span> <span class="type">long</span> data)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="comment">// Tasklet 处理逻辑</span></span><br><span class="line">    <span class="comment">// ...</span></span><br><span class="line">&#125; </span><br><span class="line"><span class="comment">// 静态初始化 tasklet</span></span><br><span class="line">DECLARE_TASKLET(my_tasklet, my_tasklet_handler, <span class="number">0</span>);</span><br><span class="line"><span class="comment">// 驱动程序的其他代码</span></span><br></pre></td></tr></table></figure>

<p>my_tasklet 是 tasklet 的名称， my_tasklet_handler 是 tasklet 的处理函数， 0是传递给处理函数的参数。 但是需要注意的是， 使用 DECLARE_TASKLET 静态初始化的 tasklet无法在运行时动态销毁， 因此在不需要 tasklet 时， 应该避免使用此方法。 如果需要在运行时销毁 tasklet， 应使用 tasklet_init 和 tasklet_kill 函数进行动态初始化和销毁， 接下来我们来学习动态初始化函数。  </p>
<h2 id="2-动态初始化函数"><a href="#2-动态初始化函数" class="headerlink" title="2. 动态初始化函数"></a><font size=3>2. 动态初始化函数</font></h2><h3 id="2-1-tasklet-init"><a href="#2-1-tasklet-init" class="headerlink" title="2.1 tasklet_init()"></a><font size=3>2.1 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/kernel/softirq.c#L548">tasklet_init()</a></font></h3><p>在 Linux 内核中， 可以使用 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/kernel/softirq.c#L548">tasklet_init()</a> 函数对 tasklet 进行动态初始化：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">void</span> <span class="title function_">tasklet_init</span><span class="params">(<span class="keyword">struct</span> tasklet_struct *t,</span></span><br><span class="line"><span class="params">		  <span class="type">void</span> (*func)(<span class="type">unsigned</span> <span class="type">long</span>), <span class="type">unsigned</span> <span class="type">long</span> data)</span></span><br><span class="line">&#123;</span><br><span class="line">	t-&gt;next = <span class="literal">NULL</span>;</span><br><span class="line">	t-&gt;state = <span class="number">0</span>;</span><br><span class="line">	<span class="type">atomic_set</span>(&amp;t-&gt;count, <span class="number">0</span>);</span><br><span class="line">	t-&gt;func = func;</span><br><span class="line">	t-&gt;data = data;</span><br><span class="line">&#125;</span><br><span class="line">EXPORT_SYMBOL(tasklet_init);</span><br></pre></td></tr></table></figure>

<p>其中， t 是指向 tasklet 结构体的指针， func 是 tasklet 的处理函数， data 是传递给处理函数的参数。</p>
<h3 id="2-2-使用实例"><a href="#2-2-使用实例" class="headerlink" title="2.2 使用实例"></a><font size=3>2.2 使用实例</font></h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;linux/interrupt.h&gt;</span></span></span><br><span class="line"><span class="comment">// 定义 tasklet 处理函数</span></span><br><span class="line"><span class="type">void</span> <span class="title function_">my_tasklet_handler</span><span class="params">(<span class="type">unsigned</span> <span class="type">long</span> data)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="comment">// Tasklet 处理逻辑</span></span><br><span class="line">    <span class="comment">// ...</span></span><br><span class="line">&#125; </span><br><span class="line"><span class="comment">// 声明 tasklet 结构体</span></span><br><span class="line"><span class="type">static</span> <span class="class"><span class="keyword">struct</span> <span class="title">tasklet_struct</span> <span class="title">my_tasklet</span>;</span></span><br><span class="line"><span class="comment">// 初始化 tasklet</span></span><br><span class="line">tasklet_init(&amp;my_tasklet, my_tasklet_handler, <span class="number">0</span>);</span><br><span class="line"><span class="comment">// 驱动程序的其他代码</span></span><br></pre></td></tr></table></figure>

<p>我们首先定义了 my_tasklet_handler 作为 tasklet 的处理函数。 然后， 声明了一个名为 my_tasklet 的 tasklet 结构体。 接下来， 通过调用 tasklet_init 函数， 进行动态初始化。  </p>
<p>通过使用 tasklet_init 函数， 我们可以在运行时动态创建和初始化 tasklet。 这样， 我们可以根据需要灵活地管理和控制 tasklet 的生命周期。 在不再需要 tasklet 时， 可以使用 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/kernel/softirq.c#L559">tasklet_kill()</a>函数进行销毁， 以释放相关资源。  </p>
<h2 id="3-改变一个tasklet的状态"><a href="#3-改变一个tasklet的状态" class="headerlink" title="3. 改变一个tasklet的状态"></a><font size=3>3. 改变一个tasklet的状态</font></h2><p>在这里，tasklet状态指两个方面：</p>
<ul>
<li>（1）state：成员所表示的运行状态；</li>
<li>（2）count：成员决定的使能／禁止状态。</li>
</ul>
<h3 id="3-1-使能-关闭状态"><a href="#3-1-使能-关闭状态" class="headerlink" title="3.1 使能&#x2F;关闭状态 "></a><font size=3>3.1 使能&#x2F;关闭状态 </font></h3><h4 id="3-1-1-tasklet-enable"><a href="#3-1-1-tasklet-enable" class="headerlink" title="3.1.1 tasklet_enable()"></a><font size=3>3.1.1 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/include/linux/interrupt.h#L615">tasklet_enable()</a></font></h4><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">static</span> <span class="keyword">inline</span> <span class="type">void</span> <span class="title function_">tasklet_enable</span><span class="params">(<span class="keyword">struct</span> tasklet_struct *t)</span></span><br><span class="line">&#123;</span><br><span class="line">	smp_mb__before_atomic();</span><br><span class="line">	<span class="type">atomic_dec</span>(&amp;t-&gt;count);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>该函数用来使能（启用） 一个已经初始化的 tasklet，该函数会把 count 增加 1。  其中， t 是指向 tasklet 结构体的指针。  使用示例如下：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;linux/interrupt.h&gt;</span></span></span><br><span class="line"><span class="comment">// 定义 tasklet 处理函数</span></span><br><span class="line"><span class="type">void</span> <span class="title function_">my_tasklet_handler</span><span class="params">(<span class="type">unsigned</span> <span class="type">long</span> data)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="comment">// Tasklet 处理逻辑</span></span><br><span class="line">    <span class="comment">// ...</span></span><br><span class="line">&#125; </span><br><span class="line"><span class="comment">// 声明 tasklet 结构体</span></span><br><span class="line"><span class="type">static</span> <span class="class"><span class="keyword">struct</span> <span class="title">tasklet_struct</span> <span class="title">my_tasklet</span>;</span></span><br><span class="line"><span class="comment">// 初始化 tasklet</span></span><br><span class="line">tasklet_init(&amp;my_tasklet, my_tasklet_handler, <span class="number">0</span>);</span><br><span class="line"><span class="comment">// 使能 tasklet</span></span><br><span class="line">tasklet_enable(&amp;my_tasklet);</span><br><span class="line"><span class="comment">// 驱动程序的其他代码</span></span><br></pre></td></tr></table></figure>

<p>我们首先定义了 my_tasklet_handler 作为 tasklet 的处理函数。 然后， 声明了一个名为 my_tasklet 的 tasklet 结构体， 并使用 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/kernel/softirq.c#L548">tasklet_init()</a> 函数对其进行初始化。 最后， 通过调用 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/include/linux/interrupt.h#L615">tasklet_enable()</a> 函数， 我们使能（启用） 了 my_tasklet。  </p>
<p>使能 tasklet 后， 如果调用 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/include/linux/interrupt.h#L588">tasklet_schedule()</a> 函数触发 tasklet， 则 tasklet 的处理函数将会被执行。 这样， tasklet 将开始按计划执行其处理逻辑。  </p>
<p>需要注意的是， 使能 tasklet 并不会自动触发 tasklet 的执行， 而是通过调用 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/include/linux/interrupt.h#L588">tasklet_schedule()</a>函数来触发。 同时， 可以使用 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/include/linux/interrupt.h#L608">tasklet_disable()</a> 函数来临时暂停或停止 tasklet 的执行。 如果需要永久停止 tasklet 的执行并释放相关资源， 则应调用 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/kernel/softirq.c#L559">tasklet_kill()</a> 函数来销毁 tasklet。  </p>
<h4 id="3-1-2-tasklet-disable"><a href="#3-1-2-tasklet-disable" class="headerlink" title="3.1.2 tasklet_disable()"></a><font size=3>3.1.2 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/include/linux/interrupt.h#L608">tasklet_disable()</a></font></h4><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">static</span> <span class="keyword">inline</span> <span class="type">void</span> <span class="title function_">tasklet_disable</span><span class="params">(<span class="keyword">struct</span> tasklet_struct *t)</span></span><br><span class="line">&#123;</span><br><span class="line">	tasklet_disable_nosync(t);</span><br><span class="line">	tasklet_unlock_wait(t);</span><br><span class="line">	smp_mb();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>该函数用来关闭 一个已经初始化的 tasklet，该函数会把 count 减 1。  其中， t 是指向 tasklet 结构体的指针。  使用示例如下：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;linux/interrupt.h&gt;</span></span></span><br><span class="line"><span class="comment">// 定义 tasklet 处理函数</span></span><br><span class="line"><span class="type">void</span> <span class="title function_">my_tasklet_handler</span><span class="params">(<span class="type">unsigned</span> <span class="type">long</span> data)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="comment">// Tasklet 处理逻辑</span></span><br><span class="line">    <span class="comment">// ...</span></span><br><span class="line">&#125; </span><br><span class="line"><span class="comment">// 声明 tasklet 结构体</span></span><br><span class="line"><span class="type">static</span> <span class="class"><span class="keyword">struct</span> <span class="title">tasklet_struct</span> <span class="title">my_tasklet</span>;</span></span><br><span class="line"><span class="comment">// 初始化 tasklet</span></span><br><span class="line">tasklet_init(&amp;my_tasklet, my_tasklet_handler, <span class="number">0</span>);</span><br><span class="line"><span class="comment">// 关闭 tasklet</span></span><br><span class="line">tasklet_disable(&amp;my_tasklet);</span><br><span class="line"><span class="comment">// 驱动程序的其他代码</span></span><br></pre></td></tr></table></figure>

<p>我们首先定义了 my_tasklet_handler 作为 tasklet 的处理函数。 然后， 声明了一个名为 my_tasklet 的 tasklet 结构体， 并使用 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/kernel/softirq.c#L548">tasklet_init()</a> 函数对其进行初始化。 最后， 通过调用 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/include/linux/interrupt.h#L608">tasklet_disable()</a> 函数， 我们关闭了 my_tasklet。  </p>
<p>关闭 tasklet 后， 即使调用 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/include/linux/interrupt.h#L588">tasklet_schedule()</a> 函数触发 tasklet，tasklet 的处理函数也不会被执行。 这可以用于临时暂停或停止 tasklet 的执行， 直到再次启用（通过调用 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/include/linux/interrupt.h#L615">tasklet_enable()</a> 函数） 。  </p>
<p>需要注意的是， 关闭 tasklet 并不会销毁 tasklet 结构体 ，因此可以随时通过调用 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/include/linux/interrupt.h#L615">tasklet_enable()</a> 函数重新启用 tasklet， 或者调用 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/kernel/softirq.c#L559">tasklet_kill()</a> 函数来销毁 tasklet。  </p>
<h3 id="3-2-改变运行状态"><a href="#3-2-改变运行状态" class="headerlink" title="3.2 改变运行状态"></a><font size=3>3.2 改变运行状态</font></h3><p>state成员中的bit[0]表示一个tasklet是否已被调度去等待执行，bit[1]表示一个tasklet是否正在某个CPU上执行。对于state变量中某位的改变必须是一个原子操作，因此可以用定义在include&#x2F;asm&#x2F;bitops.h头文件中的位操作来进行。</p>
<p>由于bit[1]这一位（即<a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/include/linux/interrupt.h#L561">TASKLET_STATE_RUN</a>）仅仅对于SMP系统才有意义，因此Linux在<a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/include/linux/interrupt.h#L561">interrupt.h</a>头文件中显示地定义了对<a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/include/linux/interrupt.h#L561">TASKLET_STATE_RUN</a>位的操作。如下所示：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">ifdef</span> CONFIG_SMP</span></span><br><span class="line"><span class="type">static</span> <span class="keyword">inline</span> <span class="type">int</span> <span class="title function_">tasklet_trylock</span><span class="params">(<span class="keyword">struct</span> tasklet_struct *t)</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="keyword">return</span> !test_and_set_bit(TASKLET_STATE_RUN, &amp;(t)-&gt;state);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">static</span> <span class="keyword">inline</span> <span class="type">void</span> <span class="title function_">tasklet_unlock</span><span class="params">(<span class="keyword">struct</span> tasklet_struct *t)</span></span><br><span class="line">&#123;</span><br><span class="line">	smp_mb__before_atomic();</span><br><span class="line">	clear_bit(TASKLET_STATE_RUN, &amp;(t)-&gt;state);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">static</span> <span class="keyword">inline</span> <span class="type">void</span> <span class="title function_">tasklet_unlock_wait</span><span class="params">(<span class="keyword">struct</span> tasklet_struct *t)</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="keyword">while</span> (test_bit(TASKLET_STATE_RUN, &amp;(t)-&gt;state)) &#123; barrier(); &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="meta">#<span class="keyword">else</span></span></span><br><span class="line"><span class="comment">//......</span></span><br><span class="line"><span class="meta">#<span class="keyword">endif</span></span></span><br></pre></td></tr></table></figure>

<p>显然，在SMP系统同，<a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/include/linux/interrupt.h#L565">tasklet_trylock()</a>宏将把一个tasklet_struct结构变量中的state成员中的bit[1]位设置成1，同时还返回bit[1]位的非。因此，如果bit[1]位原有值为1（表示另外一个CPU正在执行这个tasklet代码），那么tasklet_trylock()宏将返回值0，也就表示上锁不成功。如果bit[1]位的原有值为0，那么<a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/include/linux/interrupt.h#L565">tasklet_trylock()</a>宏将返回值1，表示加锁成功。而在单CPU系统中，<a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/include/linux/interrupt.h#L565">tasklet_trylock()</a>宏总是返回为1。</p>
<p>任何想要执行某个tasklet代码的程序都必须首先调用宏<a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/include/linux/interrupt.h#L565">tasklet_trylock()</a>来试图对这个tasklet进行上锁（即设置TASKLET_STATE_RUN位），且只能在上锁成功的情况下才能执行这个tasklet。建议！即使我们的程序只在CPU系统上运行，我们也要在执行tasklet之前调用<a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/include/linux/interrupt.h#L565">tasklet_trylock()</a>宏，以便使我们的代码获得良好可移植性。</p>
<p>在SMP系统中，<a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/include/linux/interrupt.h#L576">tasklet_unlock_wait()</a>宏将一直不停地测试TASKLET_STATE_RUN位的值，直到该位的值变为0（即一直等待到解锁），假如：CPU0正在执行tasklet A的代码，在此期间，CPU1也想执行tasklet A的代码，但CPU1发现tasklet A的TASKLET_STATE_RUN位为1，于是它就可以通过<a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/include/linux/interrupt.h#L576">tasklet_unlock_wait()</a>宏等待tasklet A被解锁（也即TASKLET_STATE_RUN位被清零）。在单CPU系统中，这是一个空操作。</p>
<p>宏<a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/include/linux/interrupt.h#L570">tasklet_unlock()</a>用来对一个tasklet进行解锁操作，也即将TASKLET_STATE_RUN位清零。在单CPU系统中，这是一个空操作。</p>
<h2 id="4-调度函数"><a href="#4-调度函数" class="headerlink" title="4. 调度函数  "></a><font size=3>4. 调度函数  </font></h2><h3 id="4-1-tasklet-schedule"><a href="#4-1-tasklet-schedule" class="headerlink" title="4.1 tasklet_schedule()"></a><font size=3>4.1 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/include/linux/interrupt.h#L588">tasklet_schedule()</a></font></h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">static</span> <span class="keyword">inline</span> <span class="type">void</span> <span class="title function_">tasklet_schedule</span><span class="params">(<span class="keyword">struct</span> tasklet_struct *t)</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="keyword">if</span> (!test_and_set_bit(TASKLET_STATE_SCHED, &amp;t-&gt;state))</span><br><span class="line">		__tasklet_schedule(t);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>该函数用来调度（触发）一个已经初始化的 tasklet，这个函数会把 tasklet 放入链表，并且设置它的 TASKLET_STATE_SCHED 状态为1。  其中， t 是指向 tasklet 结构体的指针。  </p>
<h3 id="4-2-使用实例"><a href="#4-2-使用实例" class="headerlink" title="4.2 使用实例"></a><font size=3>4.2 使用实例</font></h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;linux/interrupt.h&gt;</span></span></span><br><span class="line"><span class="comment">// 定义 tasklet 处理函数</span></span><br><span class="line"><span class="type">void</span> <span class="title function_">my_tasklet_handler</span><span class="params">(<span class="type">unsigned</span> <span class="type">long</span> data)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="comment">// Tasklet 处理逻辑</span></span><br><span class="line">    <span class="comment">// ...</span></span><br><span class="line">&#125; </span><br><span class="line"><span class="comment">// 声明 tasklet 结构体</span></span><br><span class="line"><span class="type">static</span> <span class="class"><span class="keyword">struct</span> <span class="title">tasklet_struct</span> <span class="title">my_tasklet</span>;</span></span><br><span class="line"><span class="comment">// 初始化 tasklet</span></span><br><span class="line">tasklet_init(&amp;my_tasklet, my_tasklet_handler, <span class="number">0</span>);</span><br><span class="line"><span class="comment">// 调度 tasklet 执行</span></span><br><span class="line">tasklet_schedule(&amp;my_tasklet);</span><br><span class="line"><span class="comment">// 驱动程序的其他代码</span></span><br></pre></td></tr></table></figure>

<p>我们首先定义了 my_tasklet_handler 作为 tasklet 的处理函数。 然后， 声明了一个名为 my_tasklet 的 tasklet 结构体， 并使用 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/kernel/softirq.c#L548">tasklet_init()</a> 函数对其进行初始化。 最后， 通过调用  <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/include/linux/interrupt.h#L588">tasklet_schedule()</a> 函数， 我们调度（触发） 了 my_tasklet 的执行。  </p>
<p>需要注意的是， 调度 tasklet 只是将 tasklet 标记为需要执行， 并不会立即执行 tasklet 的处理函数。 实际的执行时间取决于内核的调度和处理机制。  </p>
<h2 id="5-销毁函数"><a href="#5-销毁函数" class="headerlink" title="5. 销毁函数  "></a><font size=3>5. 销毁函数  </font></h2><h3 id="5-1-tasklet-kill"><a href="#5-1-tasklet-kill" class="headerlink" title="5.1 tasklet_kill()"></a><font size=3>5.1 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/kernel/softirq.c#L559">tasklet_kill()</a></font></h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">void</span> <span class="title function_">tasklet_kill</span><span class="params">(<span class="keyword">struct</span> tasklet_struct *t)</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="keyword">if</span> (in_interrupt())</span><br><span class="line">		pr_notice(<span class="string">&quot;Attempt to kill tasklet from interrupt\n&quot;</span>);</span><br><span class="line"></span><br><span class="line">	<span class="keyword">while</span> (test_and_set_bit(TASKLET_STATE_SCHED, &amp;t-&gt;state)) &#123;</span><br><span class="line">		<span class="keyword">do</span> &#123;</span><br><span class="line">			yield();</span><br><span class="line">		&#125; <span class="keyword">while</span> (test_bit(TASKLET_STATE_SCHED, &amp;t-&gt;state));</span><br><span class="line">	&#125;</span><br><span class="line">	tasklet_unlock_wait(t);</span><br><span class="line">	clear_bit(TASKLET_STATE_SCHED, &amp;t-&gt;state);</span><br><span class="line">&#125;</span><br><span class="line">EXPORT_SYMBOL(tasklet_kill);</span><br></pre></td></tr></table></figure>

<p>该函数用来销毁一个已经初始化的 tasklet，释放相关资源。其中， t 是指向 tasklet 结构体的指针。</p>
<ul>
<li><p>如果一个tasklet未被调度，tasklet_kill 会把它的TASKLET_STATE_SCHED 状态清 0。</p>
</li>
<li><p>如果一个 tasklet 已被调度， tasklet_kill 会等待它执行完华，再把它的 TASKLET_STATE_SCHED 状态清 0。</p>
</li>
</ul>
<h3 id="5-2-使用实例"><a href="#5-2-使用实例" class="headerlink" title="5.2 使用实例"></a><font size=3>5.2 使用实例</font></h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;linux/interrupt.h&gt;</span></span></span><br><span class="line"><span class="comment">// 定义 tasklet 处理函数</span></span><br><span class="line"><span class="type">void</span> <span class="title function_">my_tasklet_handler</span><span class="params">(<span class="type">unsigned</span> <span class="type">long</span> data)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="comment">// Tasklet 处理逻辑</span></span><br><span class="line">    <span class="comment">// ...</span></span><br><span class="line">&#125; </span><br><span class="line"><span class="comment">// 声明 tasklet 结构体</span></span><br><span class="line"><span class="type">static</span> <span class="class"><span class="keyword">struct</span> <span class="title">tasklet_struct</span> <span class="title">my_tasklet</span>;</span></span><br><span class="line"><span class="comment">// 初始化 tasklet</span></span><br><span class="line">tasklet_init(&amp;my_tasklet, my_tasklet_handler, <span class="number">0</span>);</span><br><span class="line">tasklet_disable(&amp;my_tasklet);</span><br><span class="line"><span class="comment">// 销毁 tasklet</span></span><br><span class="line">tasklet_kill(&amp;my_tasklet);</span><br><span class="line"><span class="comment">// 驱动程序的其他代码</span></span><br></pre></td></tr></table></figure>

<p>我们首先定义了 my_tasklet_handler 作为 tasklet 的处理函数。 然后， 声明了一个名为 my_tasklet 的 tasklet 结构体， 并使用 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/kernel/softirq.c#L548">tasklet_init()</a> 函数对其进行初始化。 最后， 通过调用 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/kernel/softirq.c#L559">tasklet_kill()</a> 函数， 我们我们销毁了 my_tasklet。  </p>
<p>调用 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/kernel/softirq.c#L559">tasklet_kill()</a> 函数会释放 tasklet 所占用的资源， 并将 tasklet 标记为无效。 因此， 销毁后的 tasklet 不能再被使用。  </p>
<p>需要注意的是， 在销毁 tasklet 之前， 应该确保该 tasklet 已经被停止（ 通过调用 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/include/linux/interrupt.h#L608">tasklet_disable()</a> 函数） 。 否则， 销毁一个正在执行的 tasklet 可能导致内核崩溃或其他错误。一旦销毁了 tasklet， 如果需要再次使用 tasklet， 需要重新进行初始化（通过调用  <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/kernel/softirq.c#L548">tasklet_init()</a>函数） 。 </p>
<h1 id="四、tasklet-队列"><a href="#四、tasklet-队列" class="headerlink" title="四、tasklet 队列"></a><font size=3>四、tasklet 队列</font></h1><p>多个tasklet可以通过tasklet描述符中的next成员指针链接成一个单向对列。为此，Linux定义了结构体 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/kernel/softirq.c#L462">struct tasklet_head</a> 来描述一个tasklet队列的头部指针：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment"> * Tasklets</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">tasklet_head</span> &#123;</span></span><br><span class="line">	<span class="class"><span class="keyword">struct</span> <span class="title">tasklet_struct</span> *<span class="title">head</span>;</span></span><br><span class="line">	<span class="class"><span class="keyword">struct</span> <span class="title">tasklet_struct</span> **<span class="title">tail</span>;</span></span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<p>尽管tasklet机制是特定于软中断向量<a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/include/linux/interrupt.h#L465">HI_SOFTIRQ</a>和<a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/include/linux/interrupt.h#L471">TASKLET_SOFTIRQ</a>的一种实现，但是tasklet机制仍然属于softirq机制的整体框架范围内的，因此，它的设计与实现仍然必须坚持“谁触发，谁执行”的思想。为此，Linux为系统中的每一个CPU都定义了一个tasklet队列头部，来表示应该有各个CPU负责执行的tasklet对列。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">static</span> <span class="title function_">DEFINE_PER_CPU</span><span class="params">(<span class="keyword">struct</span> tasklet_head, tasklet_vec)</span>;</span><br><span class="line"><span class="type">static</span> <span class="title function_">DEFINE_PER_CPU</span><span class="params">(<span class="keyword">struct</span> tasklet_head, tasklet_hi_vec)</span>;</span><br></pre></td></tr></table></figure>

<p>最后展开大概应该是这样的：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">tasklet_head</span> <span class="title">tasklet_vec</span>[<span class="title">NR_CPUS</span>] __<span class="title">cacheline_aligned</span>;</span></span><br></pre></td></tr></table></figure>

<p>其中， <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/kernel/softirq.c#L467">tasklet_vec[]</a>数组用于软中断向量<a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/include/linux/interrupt.h#L471">TASKLET_SOFTIRQ</a>，而<a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/kernel/softirq.c#L468">tasklet_hi_vec[]</a>数组则用于软中断向量<a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/include/linux/interrupt.h#L465">HI_SOFTIRQ</a>。</p>
<p>也即，如果CPUi（0≤i≤NR_CPUS-1）触发了软中断向量<a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/include/linux/interrupt.h#L471">TASKLET_SOFTIRQ</a>，那么对列<a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/kernel/softirq.c#L467">tasklet_vec[i]</a>中的每一个tasklet都将在CPUi服务于软中断向量<a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/include/linux/interrupt.h#L471">TASKLET_SOFTIRQ</a>时被CPUi所执行。</p>
<p>同样地，如果CPUi（0≤i≤NR_CPUS-1）触发了软中断向量<a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/include/linux/interrupt.h#L465">HI_SOFTIRQ</a>，那么队列<a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/kernel/softirq.c#L468">tasklet_hi_vec[i]</a>中的每一个tasklet都将CPUi在对软中断向量<a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/include/linux/interrupt.h#L465">HI_SOFTIRQ</a>进行服务时被CPUi所执行。</p>
<p>队列<a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/kernel/softirq.c#L467">tasklet_vec[i]</a>和<a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/kernel/softirq.c#L468">tasklet_hi_vec[i]</a>中的各个tasklet是怎样被所CPUi所执行的呢？其关键就是软中断向量<a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/include/linux/interrupt.h#L471">TASKLET_SOFTIRQ</a>和<a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/include/linux/interrupt.h#L465">HI_SOFTIRQ</a>的软中断服务程序——<a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/kernel/softirq.c#L538">tasklet_action()</a>函数和<a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/kernel/softirq.c#L543">tasklet_hi_action()</a>函数。下面我们就来分析这两个函数。</p>
<h1 id="五、使用总结"><a href="#五、使用总结" class="headerlink" title="五、使用总结"></a><font size=3>五、使用总结</font></h1><h2 id="1-使用步骤"><a href="#1-使用步骤" class="headerlink" title="1. 使用步骤"></a><font size=3>1. 使用步骤</font></h2><p>（1）声明和使用小任务大多数情况下，为了控制一个常用的硬件设备，小任务机制是实现下半部的最佳选择。小任务可以动态创建，使用方便，执行起来也比较快。我们既可以静态地创建小任务，也可以动态地创建它。选择那种方式取决于到底是想要对小任务进行直接引用还是一个间接引用。如果准备静态地创建一个小任务（也就是对它直接引用），使用下面两个宏中的一个：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">DECLARE_TASKLET(name,func, data);</span><br><span class="line">DECLARE_TASKLET_DISABLED(name,func, data);</span><br></pre></td></tr></table></figure>

<p>这两个宏都能根据给定的名字静态地创建一个tasklet_struct结构。当该小任务被调度以后，给定的函数func会被执行，它的参数由data给出。这两个宏之间的区别在于引用计数器的初始值设置不同。第一个宏把创建的小任务的引用计数器设置为0，因此，该小任务处于激活状态。另一个把引用计数器设置为1，所以该小任务处于禁止状态。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">DECLARE_TASKLET(my_tasklet, my_tasklet_handler, dev);</span><br><span class="line"><span class="comment">//这行代码其实等价于</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">tasklet_struct</span> <span class="title">my_tasklet</span> =</span> &#123; <span class="literal">NULL</span>, <span class="number">0</span>, ATOMIC_INIT(<span class="number">0</span>),  tasklet_handler, dev&#125;;</span><br></pre></td></tr></table></figure>

<p>这样就创建了一个名为my_tasklet的小任务，其处理程序为tasklet_handler，并且已被激活。当处理程序被调用的时候，dev就会被传递给它。</p>
<p>（2）编写自己的小任务处理程序小任务处理程序必须符合如下的函数类型：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">void</span>  <span class="title function_">tasklet_handler</span><span class="params">(<span class="type">unsigned</span> <span class="type">long</span> data)</span>;</span><br></pre></td></tr></table></figure>

<p>由于小任务不能睡眠，因此不能在小任务中使用信号量或者其它产生阻塞的函数。但是小任务运行时可以响应中断。</p>
<p>（3）调度自己的小任务通过调用tasklet_schedule()函数并传递给它相应的tasklt_struct指针，该小任务就会被调度以便适当的时候执行：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">tasklet_schedule(&amp;my_tasklet);        <span class="comment">/*把my_tasklet标记为挂起 */</span></span><br></pre></td></tr></table></figure>

<p>在小任务被调度以后，只要有机会它就会尽可能早的运行。在它还没有得到运行机会之前，如果一个相同的小任务又被调度了，那么它仍然只会运行一次。</p>
<p>可以调用tasklet_disable()函数来禁止某个指定的小任务。如果该小任务当前正在执行，这个函数会等到它执行完毕再返回。调用tasklet_enable()函数可以激活一个小任务，如果希望把以DECLARE_TASKLET_DISABLED()创建的小任务激活，也得调用这个函数，如：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">tasklet_disable(&amp;my_tasklet);        <span class="comment">/* 小任务现在被禁止,这个小任务不能运行 */</span></span><br><span class="line">tasklet_enable(&amp;my_tasklet);         <span class="comment">/* 小任务现在被激活 */</span></span><br></pre></td></tr></table></figure>

<p>也可以调用tasklet_kill()函数从挂起的队列中去掉一个小任务。该函数的参数是一个指向某个小任务的tasklet_struct的长指针。在小任务重新调度它自身的时候，从挂起的队列中移去已调度的小任务会很有用。这个函数首先等待该小任务执行完毕，然后再将它移去。</p>
<h2 id="2-简单示例"><a href="#2-简单示例" class="headerlink" title="2. 简单示例"></a><font size=3>2. 简单示例</font></h2><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;linux/module.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;linux/init.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;linux/fs.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;linux/kdev_t.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;linux/cdev.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;linux/kernel.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;linux/interrupt.h&gt;</span></span></span><br><span class="line"> </span><br><span class="line"><span class="type">static</span> <span class="class"><span class="keyword">struct</span>  <span class="title">t</span> <span class="title">asklet_struct</span> <span class="title">my_tasklet</span>;</span></span><br><span class="line"> </span><br><span class="line"><span class="type">static</span> <span class="type">void</span> <span class="title function_">tasklet_handler</span> <span class="params">(<span class="type">unsigned</span> <span class="type">long</span> d ata)</span></span><br><span class="line">&#123;</span><br><span class="line">    printk(KERN_ALERT,<span class="string">&quot;tasklet_handler is running./n&quot;</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">static</span> <span class="type">int</span> __init <span class="title function_">test_init</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line">    tasklet_init(&amp;my_tasklet,tasklet_handler,<span class="number">0</span>);</span><br><span class="line">    tasklet_schedule(&amp;my_tasklet);</span><br><span class="line">    return0;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">static</span>  <span class="type">void</span> __exit <span class="title function_">test_exit</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line">    tasklet_kill(&amp;tasklet);</span><br><span class="line">    printk(KERN_ALERT,<span class="string">&quot;test_exit is running./n&quot;</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">MODULE_LICENSE(<span class="string">&quot;GPL&quot;</span>);</span><br><span class="line"> </span><br><span class="line">module_init(test_init);</span><br><span class="line">module_exit(test_exit);</span><br></pre></td></tr></table></figure>

<h1 id="六、tasklet使用实例"><a href="#六、tasklet使用实例" class="headerlink" title="六、tasklet使用实例"></a><font size=3>六、tasklet使用实例</font></h1><h2 id="1-demo源码"><a href="#1-demo源码" class="headerlink" title="1. demo源码"></a><font size=3>1. demo源码</font></h2><p>源码可以看这里：<a target="_blank" rel="noopener" href="https://gitee.com/sumumm/imx6ull-driver-demo/tree/master/13_interrupt/06_nodts_tasklet">13_interrupt&#x2F;06_nodts_tasklet · 苏木&#x2F;imx6ull-driver-demo - 码云 - 开源中国</a></p>
<h2 id="2-开发板测试"><a href="#2-开发板测试" class="headerlink" title="2. 开发板测试"></a><font size=3>2. 开发板测试</font></h2><p>我们拷贝驱动到开发板，然后加载驱动：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">insmod sdriver_demo.ko</span><br></pre></td></tr></table></figure>

<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV06-%E9%A9%B1%E5%8A%A8%E5%BC%80%E5%8F%91/LV06-13-%E4%B8%AD%E6%96%AD-03-%E4%B8%AD%E6%96%AD%E4%B8%8B%E5%8D%8A%E9%83%A8-01-tasklet/img/image-20250323122800703.png" alt="image-20250323122800703" />

<p>我们按下按键，就会发现，先执行了按键中断，由于我们在按键中断中调度了tasklet，所以这里我们tasklet下半部的函数也会执行：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV06-%E9%A9%B1%E5%8A%A8%E5%BC%80%E5%8F%91/LV06-13-%E4%B8%AD%E6%96%AD-03-%E4%B8%AD%E6%96%AD%E4%B8%8B%E5%8D%8A%E9%83%A8-01-tasklet/img/image-20250323123123347.png" alt="image-20250323123123347" />



<blockquote>
<p>参考资料：</p>
<p><a target="_blank" rel="noopener" href="https://blog.csdn.net/myarrow/article/details/9287169">中断处理下半部机制-CSDN博客</a></p>
<p><a target="_blank" rel="noopener" href="https://blog.csdn.net/tiangwan2011/article/details/7254806">软中断（softirq）机制_软中断机制-CSDN博客</a></p>
</blockquote>

    </div>

    
    
    

    <footer class="post-footer">




    <div>
        
            <div style="text-align:center;color: #ccc;font-size:14px;">
            ----------本文结束
            <i class="fas fa-fan fa-spin" style="color: #FF1493; font-size: 1rem"></i>
            感谢您的阅读----------
            </div>
        
    </div>





  
  <div class="my_post_copyright"> 
    <p><span>文章标题:</span><a href="/post/765d0409.html">LV06-13-中断-03-中断下半部-01-tasklet</a></p>
    <p><span>文章作者:</span><a href="/" title="欢迎访问 《苏木》 的学习笔记">苏木</a></p>
    <p><span>发布时间:</span>2025年03月23日 - 18:40</p>
    <p><span>最后更新:</span>2025年06月14日 - 00:25</p>
    <p><span>原始链接:</span><a href="/post/765d0409.html" title="LV06-13-中断-03-中断下半部-01-tasklet">https://sumumm.github.io/post/765d0409.html</a></p>
    <p><span>许可协议:</span><i class="fab fa-creative-commons"></i> <a rel="license" href= "https://creativecommons.org/licenses/by-nc-nd/4.0/" target="_blank" title="Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0)">署名-非商业性使用-禁止演绎 4.0 国际</a> 转载请保留原文链接及作者。</p>  
  </div>
  


          <div class="post-tags">
              <a href="/tags/LV06-%E9%A9%B1%E5%8A%A8%E5%BC%80%E5%8F%91/" rel="tag"><i class="fa fa-tag"></i> LV06-驱动开发</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/post/72a70fb1.html" rel="prev" title="LV06-13-中断-03-中断下半部-02-工作队列">
                  <i class="fa fa-angle-left"></i> LV06-13-中断-03-中断下半部-02-工作队列
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/post/22e3d685.html" rel="next" title="LV06-13-中断-03-软中断">
                  LV06-13-中断-03-软中断 <i class="fa fa-angle-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">

  <div class="copyright">
    &copy; 2017 – 
    <span itemprop="copyrightYear">2025</span>
    <span class="with-love">
      <i class="fa fa-heart"></i>
    </span>
    <span class="author" itemprop="copyrightHolder">苏木</span>
  </div>
<div class="wordcount">
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-line"></i>
    </span>
      <span>站点总字数：</span>
    <span title="站点总字数">3.7m</span>
  </span>
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
      <span>站点阅读时长 &asymp;</span>
    <span title="站点阅读时长">225:26</span>
  </span>
</div>




    <span id="sitetime"></span>
    <script defer language=javascript>
        function siteTime()
        {
            window.setTimeout("siteTime()", 1000);
            var seconds = 1000;
            var minutes = seconds * 60;
            var hours = minutes * 60;
            var days = hours * 24;
            var years = days * 365;
            var today = new Date();
            var todayYear = today.getFullYear();
            var todayMonth = today.getMonth()+1;
            var todayDate = today.getDate();
            var todayHour = today.getHours();
            var todayMinute = today.getMinutes();
            var todaySecond = today.getSeconds();
            /*==================================================
            Date.UTC() -- 返回date对象距世界标准时间(UTC)1970年1月1日午夜之间的毫秒数(时间戳)
            year        - 作为date对象的年份，为4位年份值
            month       - 0-11之间的整数，做为date对象的月份
            day         - 1-31之间的整数，做为date对象的天数
            hours       - 0(午夜24点)-23之间的整数，做为date对象的小时数
            minutes     - 0-59之间的整数，做为date对象的分钟数
            seconds     - 0-59之间的整数，做为date对象的秒数
            microseconds - 0-999之间的整数，做为date对象的毫秒数
            ==================================================*/
            var t1 = Date.UTC(2017, 
                              5, 
                              19, 
                              0, 
                              0, 
                              0); //北京时间
            var t2 = Date.UTC(todayYear,todayMonth,todayDate,todayHour,todayMinute,todaySecond);
            var diff = t2-t1;
            var diffYears = Math.floor(diff/years);
            var diffDays = Math.floor((diff/days)-diffYears*365);
            var diffHours = Math.floor((diff-(diffYears*365+diffDays)*days)/hours);
            var diffMinutes = Math.floor((diff-(diffYears*365+diffDays)*days-diffHours*hours)/minutes);
            var diffSeconds = Math.floor((diff-(diffYears*365+diffDays)*days-diffHours*hours-diffMinutes*minutes)/seconds);
            document.getElementById("sitetime").innerHTML="已在这里 "+diffYears+" 年 "+diffDays+" 天 "+diffHours+" 小时 "+diffMinutes+" 分钟 "+diffSeconds+" 秒";
        }
        siteTime();
    </script>



    </div>
  </footer>

  
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up fa-lg"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/next-theme-pjax/0.6.0/pjax.min.js" integrity="sha256-vxLn1tSKWD4dqbMRyv940UYw4sXgMtYcK6reefzZrao=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.28/fancybox/fancybox.umd.js" integrity="sha256-ytMJGN3toR+a84u7g7NuHm91VIR06Q41kMWDr2pq7Zo=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/lozad.js/1.16.0/lozad.min.js" integrity="sha256-mOFREFhqmHeQbXpK2lp4nA3qooVgACfh88fpJftLBbc=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script><script src="/js/pjax.js"></script>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/hexo-generator-searchdb/1.4.1/search.js" integrity="sha256-1kfA5uHPf65M5cphT2dvymhkuyHPQp5A53EGZOnOLmc=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>




  <script src="/js/third-party/fancybox.js"></script>

  <script src="/js/third-party/pace.js"></script>


  




  

  <script class="next-config" data-name="enableMath" type="application/json">true</script><script class="next-config" data-name="mathjax" type="application/json">{"enable":true,"tags":"none","js":{"url":"https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.2/es5/tex-mml-chtml.js","integrity":"sha256-MASABpB4tYktI2Oitl4t+78w/lyA+D7b/s9GEP0JOGI="}}</script>
<script src="/js/third-party/math/mathjax.js"></script>


 
        <div id="click-show-text"
            data-mobile = false
            data-text = 富强,民主,文明,和谐,自由,平等,公正,法制,爱国,敬业,诚信,友善
            data-fontsize = 15px
            data-random= false>
        </div>
       

      
        <script async src=https://cdn.jsdelivr.net/npm/hexo-next-mouse-effect@latest/click/showText.js></script>
      

      
    




    <script async src="/js/fancybox_param.js"></script>





<!-- APlayer本体 -->



</body>
</html>
